<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-table border stripe style="margin: 10px 0px" :data="skuArr">
      <el-table-column label="序号" width="80px" align="center" type="index">
      </el-table-column>
      <el-table-column label="名称" prop="skuName"> </el-table-column>
      <el-table-column label="描述" prop="skuDesc"> </el-table-column>
      <el-table-column label="默认图片" width="180px">
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" alt="" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="重量" width="180px" prop="weight">
      </el-table-column>
      <el-table-column label="价格" width="180px" prop="price">
      </el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{ row, $index }">
          <!-- 第一个按钮 是上架或者下架的按钮 -->
          <el-button
            :type="row.isSale ? 'info' : 'primary'"
            size="small"
            :icon="row.isSale ? Bottom : Top"
            @click="updateSale(row)"
          ></el-button>
          <!-- 第二个按钮 添加的按钮 -->
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="editHandler"
          ></el-button>
          <!-- 第三个按钮 查看详情的按钮 -->
          <el-button
            type="primary"
            size="small"
            :icon="InfoFilled"
            @click="findSkuInfo(row)"
          ></el-button>
          <!--第四个按钮 删除 -->
          <el-button
            type="primary"
            size="small"
            :icon="Delete"
            @click="handlerDelete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getHasSku"
      @size-change="handler"
    />
  </el-card>
  <el-drawer title="查看商品详情" v-model="showDrawer">
    <el-divider content-position="left">商品信息</el-divider>
    <el-row :gutter="20">
      <el-col :span="3" :offset="0">名称</el-col>
      <el-col :span="12" :offset="0">{{ skuInfo.skuName }}</el-col>
    </el-row>

    <el-divider content-position="left">商品描述</el-divider>
    <el-row :gutter="20">
      <el-col :span="3" :offset="0">描述</el-col>
      <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
    </el-row>

    <el-divider content-position="left">商品价格</el-divider>
    <el-row :gutter="20">
      <el-col :span="3" :offset="0">价格</el-col>
      <el-col :span="12" :offset="0">{{ skuInfo.price }}</el-col>
    </el-row>

    <el-divider content-position="left">平台属性</el-divider>
    <el-row :gutter="20">
      <el-col :span="4" :offset="0">平台属性</el-col>
      <el-col :span="18" :offset="0">
        <el-tag
          v-for="(item, index) in skuInfo.skuAttrValueList"
          :key="item.id"
          >{{ item.valueName }}</el-tag
        >
      </el-col>
    </el-row>

    <el-divider content-position="left">销售属性</el-divider>
    <el-row :gutter="20">
      <el-col :span="4" :offset="0">销售属性</el-col>
      <el-col :span="12" :offset="0">
        <el-tag
          v-for="(item, index) in skuInfo.skuSaleAttrValueList"
          :key="item.id"
          >{{ item.saleAttrValueName }}</el-tag
        >
      </el-col>
    </el-row>

    <el-divider content-position="left">商品图片</el-divider>
    <el-row :gutter="20">
      <el-col :span="4" :offset="0">商品图片</el-col>
      <el-col :span="18" :offset="0">
        <el-carousel :interval="4000" type="card" height="200px">
          <el-carousel-item
            v-for="(item, index) in skuInfo.skuImageList"
            :key="item.id"
          >
            <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%" />
          </el-carousel-item>
        </el-carousel>
      </el-col>
    </el-row>
  </el-drawer>
</template>

<script setup lang="ts">
// 这是icon 矢量库
import { Delete, Edit, Top, Bottom, InfoFilled } from "@element-plus/icons-vue";
// 导入请求的接口
import {
  reqAllList,
  reqAllListDetail,
  reqCancelSale,
  reqSale,
} from "@/api/product/sku/index";

import { ref, onMounted, reactive } from "vue";
// 提示信息的组件
import { ElMessage } from "element-plus";
// 当前的页码数
const pageNo = ref<number>(1);
// 每页的条数
const pageSize = ref<number>(11);
// 创建存储全部的sku
const skuArr = ref<any>([]);
// 总条数
const total = ref<number>(0);
// 定义抽屉的展示或者隐藏
const showDrawer = ref<boolean>(false);
// 存储sku信息
const skuInfo = reactive<any>({});
// 发送请求
const getHasSku = async (pager: number = 1) => {
  // 收集用户当前选中的页码
  pageNo.value = pager;
  // 发送请求
  let result = await reqAllList(pageNo.value, pageSize.value);
  // 保存数据
  skuArr.value = result.records;
  // 保存总条数
  total.value = result.total;
};
// 生命周期的挂载
onMounted(() => {
  getHasSku();
});
// 分页器的下拉框的回调函数
const handler = (size: number) => {
  // 整理参数
  pageSize.value = size;
  // 重新发送请求
  getHasSku();
};
// 商品上架和下架的回调函数
const updateSale = async (row: any) => {
  // 判断用户现在是上架还是下架
  if (row.isSale) {
    await reqCancelSale(row.id);
    // 上架和下架的提示信息
    ElMessage({
      type: "success",
      message: "下架成功",
    });
  } else {
    await reqSale(row.id);
    // 上架的提示信息
    ElMessage({
      type: "success",
      message: "上架成功",
    });
  }
  // 更新完成重新的发送请求
  getHasSku(pageNo.value);
};
//第二个按钮的回调
const editHandler = () => {
  ElMessage({
    type: "success",
    message: "程序员在努力开发中....",
  });
};
// 第三个按钮 是详情页面的查看
const findSkuInfo = async (row: any) => {
  //抽屉展示出来
  showDrawer.value = true;
  // 获取商品展示的数据
  let result = await reqAllListDetail(row.id);
  // 收集并保存
  Object.assign(skuInfo, result);
};
// 第四个按钮 删除按钮的回调函数
const handlerDelete = () => {
  ElMessage({
    type: "success",
    message: "程序正在打理中....",
  });
};
</script>

<script lang="ts">
export default {
  name: "Sku",
};
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
